package com.ruoyi.common.utils.file;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextSegmenter {

    private static final int CHUNK_SIZE = 1500;
    private static final int PREVIOUS_CHUNK_TAIL_SIZE = 200;
    private static final String SEPARATOR = "^^^";

    public static String segmentText(String longText) {
        List<String> chunks = new ArrayList<>();
        int startIndex = 0;

        while (startIndex < longText.length()) {
            int endIndex = Math.min(startIndex + CHUNK_SIZE, longText.length());

            // 检查分段结尾是否是图片链接
            if (endIndex < longText.length() && longText.charAt(endIndex) == '!') {
                int imageLinkEnd = findImageLinkEnd(longText, endIndex);
                if (imageLinkEnd != -1) {
                    endIndex = imageLinkEnd;
                }
            }

            String chunk = longText.substring(startIndex, endIndex);

            // 如果不是第一个分段，添加上一个分段结尾的200个字符
            if (!chunks.isEmpty()) {
                int previousChunkEnd = startIndex - PREVIOUS_CHUNK_TAIL_SIZE;
                String previousChunkTail = longText.substring(previousChunkEnd, startIndex);
                chunk = previousChunkTail + chunk;
            }

            chunks.add(chunk);
            startIndex = endIndex;
        }

        // 在分段之间添加标识符
        for (int i = 1; i < chunks.size(); i++) {
            chunks.set(i, SEPARATOR + chunks.get(i));
        }

        // 将分段拼接成一个完整的字符串
        StringBuilder texts = new StringBuilder();
        for (String segment : chunks) {
            texts.append(segment);
        }

        return texts.toString();
    }

    public static String segmentText(String longText,String fileName) {
        List<String> chunks = new ArrayList<>();
        int startIndex = 0;

        while (startIndex < longText.length()) {

            int endIndex = Math.min(startIndex + CHUNK_SIZE, longText.length());

            // 检查分段结尾是否是图片链接
            if (endIndex < longText.length() && longText.charAt(endIndex) == '!') {
                int imageLinkEnd = findImageLinkEnd(longText, endIndex);
                if (imageLinkEnd != -1) {
                    endIndex = imageLinkEnd;
                }
            }

            String chunk = longText.substring(startIndex, endIndex);

            // 如果不是第一个分段，添加上一个分段结尾的200个字符
            if (!chunks.isEmpty()) {
                int previousChunkEnd = startIndex - PREVIOUS_CHUNK_TAIL_SIZE;
                String previousChunkTail = longText.substring(previousChunkEnd, startIndex);
                chunk = previousChunkTail + chunk;
            }

            chunks.add(chunk);
            startIndex = endIndex;
        }

        // 在分段之间添加标识符
        for (int i = 1; i < chunks.size(); i++) {
            chunks.set(i, SEPARATOR+fileName+"\n"+chunks.get(i));
        }

        // 将分段拼接成一个完整的字符串
        StringBuilder texts = new StringBuilder();
        texts.insert(0,fileName+"\n");
        for (String segment : chunks) {
            texts.append(segment);
        }

        return texts.toString();
    }

    private static int findImageLinkEnd(String text, int startIndex) {
        int endIndex = startIndex;
        while (endIndex < text.length() && text.charAt(endIndex) != ')') {
            endIndex++;
        }
        return endIndex + 1; // 返回图片链接的结尾位置
    }

    public static String processChapters(String text) {
        // 使用正则表达式匹配章节信息
        Pattern pattern = Pattern.compile("第\\s*[一二三四五六七八九十]+\\s*章\\s*.*");
        Matcher matcher = pattern.matcher(text);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "^^^" + matcher.group());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

}
